<template>
  <div id="QuestionResource">
    <a-form layout="inline" style="margin-bottom: 8px">
      <a-form-item label="选择课程" class="searchItem">
        <a-select v-model="course" placeholder="请输入" style="width: 200px">
          <a-select-option
            v-for="item in courseList"
            :key="item.id"
            :value="item.name"
            >{{ item.name }}</a-select-option
          >
        </a-select>
      </a-form-item>
      <a-form-item label="关键字" class="searchItem">
        <a-input v-model="keyword" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="">
        <a-button type="primary" @click="getData"> 查询 </a-button>
      </a-form-item>
    </a-form>
    <div style="display: flex; justify-content: flex-end">
      <a-upload
        class="upload"
        name="file"
        :multiple="true"
        :show-upload-list="false"
        action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
      >
        <a-button icon="cloud-upload">上传</a-button>
      </a-upload>
      <a-button type="primary" style="margin-left: 10px" @click="addbtn">新增</a-button>
      <a-button type="primary" style="margin-left: 10px">导出</a-button>
      <!-- <a-button type="primary" style="margin-left: 10px">删除</a-button> -->
    </div>
    <a-table
      :columns="columns"
      :scroll="{ y: 500 }"
      style="margin-top: 30px"
      :data-source="tableData"
      :pagination="tablePagination"
      :rowKey="(record, index) => index"
      :row-selection="{
        selectedRowKeys: selectedRowKeys,
        onChange: onSelectChange,
      }"
    >
      <template slot="name" slot-scope="text, record">
        <a-tooltip placement="top" v-if="record.name">
          <template slot="title">
            <span>{{ record.name }}</span>
          </template>
          <span style="cursor: default">{{ record.name }}</span>
        </a-tooltip>
        <span v-else-if="!record.name">—</span>
      </template>
      <template slot="tapdProjectId" slot-scope="text, record">
        <a-tooltip placement="top" v-if="record.tapdProjectId">
          <template slot="title">
            <p>{{ record.tapdProjectId }}</p>
          </template>
          <span style="cursor: default">{{ record.tapdProjectId }}</span>
        </a-tooltip>
        <span v-else-if="!record.tapdProjectId">—</span>
      </template>
      <template slot="operation" slot-scope="text, record">
        <a-button
          type="primary"
          @click="editClick(record)"
          style="margin-right: 10px"
          >修改</a-button
        >
        <a-button type="danger" @click="deleteClick(record)">删除</a-button>
      </template>
    </a-table>
    <a-modal v-model="visible" :title="title" @cancel="handleCancel">
      <a-form-model
        :label-col="{ span: 5 }"
        :wrapper-col="{ span: 12 }"
        ref="formRef"
        :model="form"
        :rules="rules"
      >
        <a-form-model-item label="课程名称" prop="courseName">
          <a-select v-model="form.courseName">
            <a-select-option
              v-for="item in courseNameList"
              :key="item.id"
              :value="item.id"
              >{{ item.name }}</a-select-option
            >
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="分数" prop="score">
          <a-input v-model="form.score" />
        </a-form-model-item>
        <a-form-model-item label="试题内容" prop="topic">
          <a-input v-model="form.topic" />
        </a-form-model-item>
        <a-form-model-item label="试题类型" prop="type">
          <a-select v-model="form.type">
            <a-select-option
              v-for="item in typeList"
              :key="item.id"
              :value="item.id"
              >{{ item.label }}</a-select-option
            >
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="选项A" prop="optionA">
          <a-input v-model="form.optionA" />
        </a-form-model-item>
        <a-form-model-item label="选项B" prop="optionB">
          <a-input v-model="form.optionB" />
        </a-form-model-item>
        <a-form-model-item label="选项C" prop="optionC">
          <a-input v-model="form.optionC" />
        </a-form-model-item>
        <a-form-model-item label="选项D" prop="optionD">
          <a-input v-model="form.optionD" />
        </a-form-model-item>
        <a-form-model-item label="答案" prop="answer">
          <a-input v-model="form.answer" />
        </a-form-model-item>
      </a-form-model>
      <template slot="footer">
        <a-button key="back" class="modalCancel" @click="handleCancel"
          >取消</a-button
        >
        <a-button key="submit" @click="primaryClick" type="primary">
          确定
        </a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>
export default {
  data() {
    return {
      typeList:[
        {
          id:1,
          label:"判断题",
        },
        {
          id:2,
          label:"单选题",
        },
        {
          id:3,
          label:"多选题",
        },
      ],
      courseNameList:[],
      courseList: [], //课程下拉列表
      course: null, //课程所选中的值
      keyword: null, //关键字
      form: {
        courseName: "", // 课程名称
        topic: "", //试题内容
        type: "", //试题类型
        optionA: "", //选项A
        optionB: "", //选项B
        optionC: "", //选项C
        optionD: "", //选项D
        answer: "", //答案
      },
      rules: {
        courseName: [
          { required: true, message: "请输入课程名称", trigger: "blur" },
        ],
        topic: [{ required: true, message: "请输入试题内容", trigger: "blur" }],
        score: [{ required: true, message: "请输入分数", trigger: "blur" }],
        type: [{ required: true, message: "请选择试题类型", trigger: "blur" }],
        optionA: [{ required: true, message: "请输入选项A", trigger: "blur" }],
        optionB: [{ required: true, message: "请输入选项B", trigger: "blur" }],
        optionC: [{ required: true, message: "请输入选项C", trigger: "blur" }],
        optionD: [{ required: true, message: "请输入选项D", trigger: "blur" }],
        answer: [{ required: true, message: "请输入答案", trigger: "blur" }],
      },
      visible: false,
      title: null,
      selectedRowKeys: [],
      columns: [
        //主页table列的配置
        {
          title: "课程名称",
          dataIndex: "courseName",
          customRender: (text, record) => {
            return record.courseName ? record.courseName : "—";
          },
        },
        {
          title: "试题内容",
          dataIndex: "topic",
          customRender: (text, record) => {
            return record.topic ? record.topic : "—";
          },
        },
        {
          title: "试题类型",
          dataIndex: "type",
          //1是判断,2是单选,3是多选
          customRender: (text, record) => {
            return record.type ? record.type : "—";
          },
        },
        {
          title: "选项A",
          dataIndex: "optionA",
          customRender: (text, record) => {
            return record.optionA ? record.optionA : "—";
          },
        },
        {
          title: "选项B",
          dataIndex: "optionB",
          customRender: (text, record) => {
            return record.optionB ? record.optionB : "—";
          },
        },
        {
          title: "选项C",
          dataIndex: "optionC",
          customRender: (text, record) => {
            return record.optionC ? record.optionC : "—";
          },
        },
        {
          title: "选项D",
          dataIndex: "optionD",
          customRender: (text, record) => {
            return record.optionD ? record.optionD : "—";
          },
        },
        {
          title: "答案",
          dataIndex: "answer",
          customRender: (text, record) => {
            return record.answer ? record.answer : "—";
          },
        },
        {
          title: "操作",
          width: 180,
          dataIndex: "operation",
          scopedSlots: { customRender: "operation" },
        },
      ],
      tableData: [
        //主页表格的数据
      ],
      tablePagination: {
        //主页表格的分页器
        total: 0,
        current: 1,
        size: "10",
        showQuickJumper: true, //是否可以快速跳转至某页
        showSizeChanger: true, //是否可以改变 pageSize
      },
    };
  },
  created() {
    this.getData(); //获取列表数据
    this.getCourseList(); //获取课程列表的下拉
    this.getCourseNameList();//获取课程下拉
  },
  methods: {
    getCourseNameList(){
      this.$axios.get("/sys/course/page").then(res=>{
        this.courseNameList=res.response;
      })
    },
    addbtn() {
      this.title = "新增";
      this.visible = true;
    },
    onSelectChange(selectedRowKeys) {
      console.log("selectedRowKeys changed: ", selectedRowKeys);
      this.selectedRowKeys = selectedRowKeys;
    },
    editClick(row) {
      //编辑
      for (let i in row) {
        if (i in this.form) {
          this.form[i] = row[i];
        }
      }
      this.title = "编辑";
      this.visible = true;
    },
    handleCancel() {
      this.form = {
        courseName: "", // 课程名称
        score:"",//分数
        topic: "", //试题内容
        type: "", //试题类型
        optionA: "", //选项A
        optionB: "", //选项B
        optionC: "", //选项C
        optionD: "", //选项D
        answer: "", //答案
      };
      this.visible = false;
    },
    primaryClick() {
      this.$nextTick(() => {
        let data =  {
        courseId: this.form.courseName, // 课程名称
        score:this.form.score,//分数
        topic: this.form.topic, //试题内容
        type: this.form.type, //试题类型
        optionA: this.form.optionA, //选项A
        optionB: this.form.optionB, //选项B
        optionC: this.form.optionC, //选项C
        optionD: this.form.optionD, //选项D
        answer: this.form.answer, //答案
      };
        this.$refs.formRef.validate((valid) => {
          if (valid) {
            if (this.title == "新增") {
              this.$axios.post("/sys/question", data).then((res) => {
                this.$message.success("新增成功");
                this.getData();
                this.handleCancel();
              });
            } else if (this.title == "编辑") {
              this.$axios.put("/sys/question", data).then((res) => {
                this.$message.success("编辑成功");
                this.getData();
                this.handleCancel();
              });
            }
          } else {
            console.log("校验失败");
          }
        });
      });
    },
    async getData() {
      let data = await this.$axios.get("/sys/question/page", {
        // params: {
        //   page: this.tablePagination.current,
        //   limit: this.tablePagination.size,
        //   courseName: this.course, //课程名称
        //   questionTopic: this.keyword, //关键字(也是题目内容)
        // },
      });
      this.tableData = data.response;
      this.tablePagination.total = data.response.length;
    },
    deleteClick(row) {
      //点击删除
      let params = {
        ids: row.id,
      };
      let that = this;
      this.$confirm({
        title: "确认删除?",
        content: "您确定要删除此项吗?",
        onOk() {
          that.$axios.delete("/sys/question", {params}).then((res) => {
            that.$message.success("删除成功");
            that.getData();
          });
        },
        onCancel() {
          console.log("取消删除");
        },
      });
    },
    async getCourseList() {
      //获取课程的下拉列表
      let data = await this.$axios.get("/sys/course/page");
      this.courseList = data.response;
    },
  },
};
</script>

<style lang="scss">
#QuestionResource {
  // .upload {
  //   position: relative;
  //   .ant-upload-list {
  //     position: absolute;
  //     right: 0;
  //     .ant-upload-list-item {
  //       width: 200px;
  //     }
  //   }
  // }
}
</style>